package com.xbongbong.saas.toolbox.help;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.saas.enums.OnlyInsertDBEnum;

import java.util.List;
import java.util.Objects;

/**
 * 只入数据库未入es help类
 *
 * @Author: hongxiao
 * @date: 2021-11-15 22:21
 */
public class OnlyInsertDBHelp {

    /**
     * 过虑不入es的数据
     *
     * @param businessType 业务类型
     * @param data 数据data
     */
    public static void filterData(Integer businessType, Integer distributorMark, JSONObject data) {
        if (Objects.isNull(data)) {
            return;
        }
        // es保存时移除头像字段，和鲁江确认过
        if (OnlyInsertDBEnum.containBusinessType(businessType, distributorMark)) {
            List<OnlyInsertDBEnum> dbEnums = OnlyInsertDBEnum.getByBusinessType(businessType, distributorMark);
            for (OnlyInsertDBEnum dbEnum : dbEnums) {
                data.remove(dbEnum.getAttr());
            }
        }
    }

    /**
     * 设置数据库数据
     *
     * @param businessType 业务类型
     * @param dbData 数据库data
     * @param data 数据data
     */
    public static void setData(Integer businessType, Integer distributorMark, JSONObject dbData, JSONObject data) {
        if (OnlyInsertDBEnum.containBusinessType(businessType, distributorMark)) {
            List<OnlyInsertDBEnum> dbEnums = OnlyInsertDBEnum.getByBusinessType(businessType, distributorMark);
            for (OnlyInsertDBEnum dbEnum : dbEnums) {
                data.put(dbEnum.getAttr(), dbData.getOrDefault(dbEnum.getAttr(), ""));
            }
        }
    }

}
